/* 
 * File:   network.h
 * Author: tangchao
 *
 * Created on 2013年6月17日, 下午2:16
 */

#ifndef DP_NETWORK_H
#define	DP_NETWORK_H

#ifdef	__cplusplus
extern "C"
{
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include <fcntl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>

#define socket_errno	errno
#define SOCKET_EADDRINUSE EADDRINUSE
#define SOCKET_EINTR	EINTR
#define SOCKET_EAGAIN	EAGAIN
#define SOCKET_ENFILE	ENFILE
#define SOCKET_EMFILE	EMFILE

#define INVALID_SOCKET  -1

#define TEST_BLOCKING		8

#define MAX_ACCEPT_RETRY	10

#define ER_ERROR_FIRST 1000
#define ER_IPSOCK_ERROR 1081

#define closesocket(A)	close(A)

    enum enum_vio_type
    {
        VIO_CLOSED, VIO_TYPE_TCPIP, VIO_TYPE_SOCKET, VIO_TYPE_NAMEDPIPE,
        VIO_TYPE_SSL, VIO_TYPE_SHARED_MEMORY
    };

    typedef struct ST_VIO VIO;

    struct ST_VIO
    {
        int sd; /* my_socket - real or imaginary */
        int fcntl_mode; /* Buffered fcntl(sd,F_GETFL) */
        struct sockaddr_storage local; /* Local internet address */
        struct sockaddr_storage remote; /* Remote internet address */
        int addrLen; /* Length of remote address */
        enum enum_vio_type type; /* Type of connection */
        char desc[30]; /* String description */
    };


    typedef struct _THD THD;

    struct _THD
    {
        int network_sock;
        char *query;
        ulong query_length;
        VIO *net;
    };

    void network_init(void);
    VIO* vio_new(int sd, enum enum_vio_type type, uint flags);
    void handle_connections_sockets(void);
    bool my_net_init(VIO *net, VIO* vio);


#ifdef	__cplusplus
}
#endif

#endif	/* NETWORK_H */

